/* 
 * Copyright (c) 2009-2012, Newcastle University, UK.
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met: 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE. 
 * KL 29-03-2011 
 */

/* Uses the internal RTCC of the PIC24 */
#ifndef MYRTC_H
#define MYRTC_H


// Includes
#include <Rtcc.h>

// Globals
extern rtccTimeDate TheTime;
/* Typical usage example
sprintf(my_string,"%02u/%02u/%02u", FromBcd(TheTime.f.mday),FromBcd(TheTime.f.mon),FromBcd(TheTime.f.year));
sprintf((my_string+10),"%02u:%02u:%02u", FromBcd(TheTime.f.hour),FromBcd(TheTime.f.min),FromBcd(TheTime.f.sec));
*/

//Defines
// (Private) Convert a value from BCD - do not use this macro where evaluating the parameter has a side effect (e.g. a function call reading a value)
//static unsigned char FromBcd(unsigned char value) { return ((value & 0xf0) >> 4) * 10 + (value & 0x0f); }
#define FromBcd(_value) ((((_value) & 0xf0) >> 4) * 10 + ((_value) & 0x0f))   
// (Private) Convert a value to BCD - do not use this macro where evaluating the parameter has a side effect (e.g. a function call reading a value)
//static unsigned char ToBcd(unsigned char value) { return ((value / 10) << 4) | (value % 10); }
#define ToBcd(_value) ((((_value) / 10) << 4) | ((_value) % 10))
// BCD Nibble to ASCII low and high nibbles - NUMERALS ONLY NOT HEX
#define my_BCDtoaL(_nibble)  ((_nibble&0xf)+'0')
#define my_BCDtoaH(_nibble)  ((_nibble>>4)+'0')

// RTC init
extern void InitRTC(void); 	// Call first
extern void SetTime(void);		// Call to update the module from the globals
extern void UpdateTime(void);	// Call to update the globals
extern void ClearRTC(void);
extern void SetupAlarm (void);
extern void SetupChimeOn (void); // Note: Chime enable just lets the alarm keep repeating indefinitely, still need one alarm to start it.
extern void SetupChimeOff (void); 
extern void TurnOnRTCInt(void);
extern void TurnOffRTCInt(void);

extern unsigned long PackTime (void); // Update time stamp first
extern rtccTimeDate* DateTimeFromString(const char *value);
extern const char *DateTimeToString(rtccTimeDate* value);

extern void __attribute__((interrupt,auto_psv)) _RTCCInterrupt(void);
#endif
